define(function (require, exports) {
	/**
	 * 页面加载完成时触发
	 * @param hashCode 路由ID
	 * @param data 传递数据对象
	 * @param cite 页面站点信息
	 */
	exports.ready = function (hashCode, data, cite) {
		yufp.lookup.reg('USE_TASK_STATE,USE_ALERTM_MODE');
		yufp.custom.vue({
			el: cite.el,
			data: function () {
				var eventTypeOption = [
					{ key: 'G', value: '普通事件' },
					{ key: 'F', value: '文件事件' }];
				var _this = this;
				/*定义校验规则，只能填字母数字 下划线*/
				var valiCodes = function (rule, value, callback) {
					var reg = /^[0-9a-zA-Z_]{1,}$/;
					if (value && reg.test(value)) {
						if (value.length > 64) {
							callback(new Error('长度不能大于64位'));
						} else {
							callback();
						}
					} else if (value && !reg.test(value)) {
						callback(new Error('能填字母、数字和下划线'));
					} else {
						callback();
					}
				};
				/**检验数字为0-255*/
				var numMax = function (rule, value, callback) {
					if (value) {
						if (Number(value) > 0 && Number(value) <= 100) {
							callback();
						} else {
							callback(new Error('返回值必须为>0且<=100'));
						}
					} else {
						callback();
					}
				};
				var numMaxOne = function (rule, value, callback) {
					if (value) {
						if (Number(value) > 0) {
							callback();
						} else {
							callback(new Error('返回值必须为>0'));
						}
					} else {
						callback();
					}
				};

				return {
					tbHeight:'0',
					searchFormdata: {},
					objectUserFormdata: {},
					objectUserdFormdata: {},
					resCodeOption: [],
					addFnButton: !yufp.session.checkCtrl('addFn'),
					updateFnButton: !yufp.session.checkCtrl('updateFn'),
					deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
					treeDataUrl: "api/use/plats/paramValue/queryWarningResourceTree",
					dataUrl: 'api/use/warning/resource/Resource',
					objDataUrl: "api/use/warnings/job/queryUseAlarmTargetList",
					objDataUserUrl: "api/adminsmuser/querypage",
					groupDataUrl: "api/use/warnings/groups/queryList",//对象组列表查询
					groupTabVisible: false,//对象组dialog展示标志
					selectCount: 0,
					alarmClassOptions: [],
					resourceCodeOptions: [],
					resourceAddrOptions: [],
					objDatas: [],
					templateDatas: [],
					currTemplateDatas: [],
					templateDatasClass: [[], [], [], []], //0,代表手机短信，1代表电子邮件 2. 代表控制台 3. 对象组
					baseParam: { condition: { orgId: yufp.session.org.id } },
					validFlagOptions: [{ key: 'Y', value: '启用' }, { key: 'N', value: '停用' }],
					changeBtnShow: true,
					deleteBtnShow: true,
					addBtnShow: false,
					hintDialogVisible: false,
					objectTabVisible: false,
					objectUserTabVisible: false,
					disfalse: true,//是否可以编辑对象信息
					currentClickNode: {},
					formdata: {},
					addFlag: true,
					activeTabName: 'basicInfo',
					async: false,
					param: { UNITID: '0000', LEVELUNIT: '1' },
					height: yufp.frame.size().height - 117,
					dialogVisible: false,
					eventTypeOption: eventTypeOption,
					viewTitleArrry: ['警示条件添加', '警示条件修改', "警示条件查看"],
					viewTitle: '',
					isAppNode: false,
					isView: false,
					currAppNode: {},
					rules: [{ required: true, message: '必填项' }],
					rulesCode: [
						{ validator: valiCodes, trigger: 'blur' }
					],
					integerRulesMax: [{ required: true, message: '必填项' },
					{ message: '请输入数字', validator: yufp.validator.number },
					{ validator: numMax, trigger: 'blur' }
					],
					integerRulesOne: [{ required: true, message: '必填项' },
					{ message: '请输入数字', validator: yufp.validator.number },
					{ validator: numMaxOne, trigger: 'blur' }
					],
					integerRules: [{ required: true, message: '必填项' },
					{ message: '请输入数字', validator: yufp.validator.number },
					],
					queryObjFields: [
						{ placeholder: '报警方式', field: 'alarmMode', type: 'select', dataCode: "USE_ALERTM_MODE" },
						{ placeholder: '对象名称', field: 'targetName', type: 'input' }],
					alarmModeDatas: [
						{ key: 'S', value: '手机短信' },
						{ key: 'M', value: '电子邮件' },
						{ key: 'N', value: '控制台通知' },
						{ key: '+', value: '对象组' }
					],
					//警示对象组查询条件
					queryGroupFields: [
						{ placeholder: '警示对象组编号', field: 'groupCode', type: 'input' },
						{ placeholder: '警示对象组名称', field: 'groupName', type: 'input' }
					],
					//警示对象组处理按钮
					queryGroupButtons: [
						{
							label: '搜索', op: 'submit', type: 'primary', click: function (model, valid) {
								_this.$refs.refGroupUsedTable.remoteData({ condition: model });
							}
						},
						{
							label: '确认', op: 'submit', type: 'primary', click: function () {
								var selectDatas = _this.$refs.refGroupUsedTable.selections;
								for (var i = 0; i < selectDatas.length; i++) {
									var isContain = false;
									for (var j = 0; j < _this.objDatas.length; j++) {
										if (selectDatas[i].targetCode === _this.objDatas[j].targetCode
											&& selectDatas[i].alarmMode === _this.objDatas[j].alarmMode) {
											isContain = true;
											break;
										}
									}
									if (!isContain) {
										var obj = {};
										obj.alarmMode = selectDatas[i].alarmMode;
										obj.targetName = selectDatas[i].groupName;
										obj.targetCode = selectDatas[i].groupCode;
										_this.objDatas.push(obj);
									}
								}
								_this.$refs.refGroupUsedTable.selections.splice(0);
								_this.groupTabVisible = false;
							}
						}
					],
					queryObjButtons: [
						{
							label: '搜索', op: 'submit', type: 'primary', click: function (model, valid) {
								_this.$refs.refObjUsedTable.remoteData({ condition: model });
							}
						},
						{
							label: '确认', op: 'submit', type: 'primary', click: function () {
								let selectDatas = _this.$refs.refObjUsedTable.selections;
								for (var i = 0; i < selectDatas.length; i++) {
									var isContain = false;
									for (var j = 0; j < _this.objDatas.length; j++) {
										if (selectDatas[i].targetCode === _this.objDatas[j].targetCode
											&& selectDatas[i].alarmMode === _this.objDatas[j].alarmMode) {
											isContain = true;
											break;
										}
									}
									if (!isContain) {
										var obj = {};
										obj.alarmMode = selectDatas[i].alarmMode;
										obj.targetName = selectDatas[i].targetName;
										obj.targetData = selectDatas[i].targetData;
										obj.targetCode = selectDatas[i].targetCode;
										_this.objDatas.push(obj);
									}
								}
								_this.$refs.refObjUsedTable.selections.splice(0);
								_this.objectTabVisible = false;
							}
						}
					],
					queryObjUserFields: [{ placeholder: '登录代码/姓名/员工号', field: 'userInfo', type: 'input' }],
					queryObjUserButtons: [
						{
							label: '搜索', op: 'submit', type: 'primary', click: function (model, valid) {
								model.orgId = yufp.session.org.id;
								_this.$refs.refObjUserTable.remoteData({ condition: model });
							}
						},
						{
							label: '确认', op: 'submit', type: 'primary', click: function () {
								let selectDatas = _this.$refs.refObjUserTable.selections;
								for (var i = 0; i < selectDatas.length; i++) {
									var obj = {};
									obj.alarmMode = 'N';
									obj.targetName = selectDatas[i].userName;
									obj.targetData = selectDatas[i].loginCode;
									_this.objDatas.push(obj);
								}
								_this.$refs.refObjUserTable.selections.splice(0);
								_this.objectUserTabVisible = false;
							}
						}
					],
				};
			},
			mounted: function () {
				var _this = this;
				//测试
				_this.$nextTick(function(){
						var tableBox = _this.$refs.tableBox;
						let a = tableBox.getBoundingClientRect().top + 28;
						var theight = `calc(100vh - ${a}px - 64px)`;
						_this.tbHeight = theight;		
				});
				_this.initQueryField();
				_this.initOption();
				_this.initresourceAddrOptions();
			},
			methods: {

				reloadData: function () {

					var _this = this;
					var paramObj = _this.searchFormdata;
					paramObj.alarmClass = _this.currentClickNode.alarmClass;
					_this.$refs.refTable.remoteData({ condition: paramObj });

				},

				initQueryField: function () {
					var _this = this;
					yufp.service.request({
						method: 'GET',
						url: "api/use/warning/resource/ResourceList",
						async: false,
						callback: function (code, message, response) {
							if (response.code !== 0) {
								_this.$message(response.message);
							} else {
								for (let i = 0; i < response.data.length; i++) {
									_this.resCodeOption.push({ key: response.data[i].resourceCode, value: response.data[i].resourceName });
								}
							}

						}
					});
				},

				initresourceAddrOptions: function () {
					var _this = this;
					yufp.service.request({
						method: 'GET',
						url: "api/use/plats/apps/getAllIps",
						data: {},
						async: false,
						callback: function (code, message, response) {
							if (response.code !== 0) {
								_this.$message(response.message);
							} else {
								_this.resourceAddrOptions.splice(0);
								_this.resourceAddrOptions.push({ key: " ", value: "全部" });
								for (let i = 0; i < response.data.length; i++) {
									_this.resourceAddrOptions.push({ key: response.data[i], value: response.data[i] });
								}
							}

						}
					});
				},

				initOption: function () {
					var _this = this;
					yufp.service.request({
						method: 'GET',
						url: "api/use/plats/paramValue/queryWarningResourceTree",
						data: {},
						async: false,
						callback: function (code, message, response) {
							if (response.code !== 0) {
								_this.$message(response.message);
							} else {
								for (let i = 0; i < response.data.length; i++) {
									_this.alarmClassOptions.push({ key: response.data[i].paramCode, value: response.data[i].paramName });
								}
							}

						}
					});
					if (_this.addFlag) {
						urlparam = 'api/use/plats/paramValue/';
					} else {
						urlparam = 'api/use/plats/paramValue/queryWarningResourceTree';
					}
					yufp.service.request({
						method: 'GET',
						url: "api/use/warning/resource/ResourceList",
						data: {},
						async: false,
						callback: function (code, message, response) {
							if (response.code != 0) {
								_this.$message(response.message);
							} else {
								for (let i = 0; i < response.data.length; i++) {
									_this.resourceCodeOptions.push({ key: response.data[i].resourceCode, value: response.data[i].resourceName });
								}
							}

						}
					});
				},
				addEvent: function () {
					var _this = this;
					_this.dialogVisible = true;
					_this.activeTabName = "basicInfo";
					_this.viewTitle = _this.viewTitleArrry[0];
					_this.isReadOnly = false;
					_this.isView = false;
					_this.addFlag = true;
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						_this.objDatas = [];
						_this.formdata.alarmClass = _this.currentClickNode.paramCode;
						_this.queryTemplateDatas(_this.currentClickNode.paramCode);
					});

				},

				viewDataFn: function (data) {
					var _this = this;
					_this.activeTabName = 'basicInfo';

					_this.dialogVisible = true;
					_this.viewTitle = _this.viewTitleArrry[2];
					_this.isReadOnly = false;
					_this.addFlag = false;
					_this.isView = true;
					var val = data;
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						_this.formdata.alarmClass = val.alarmClass;
						_this.formdata.ruleCode = val.ruleCode;
						_this.formdata.ruleName = val.ruleName;
						_this.formdata.resourceCode = val.resourceCode;
						_this.formdata.resourceAddr = val.resourceAddr;
						_this.formdata.waterLimit = val.waterLimit;
						_this.formdata.timeLimit = val.timeLimit;
						_this.formdata.validFlag = val.validFlag;
						_this.formdata.remark = val.remark;
						_this.queryTemplateDatas(data.alarmClass);
						_this.queryLoadDataObj(data);
					});

				},
				changeEvent: function () {
					var _this = this;
					_this.activeTabName = 'basicInfo';
					_this.viewTitle = _this.viewTitleArrry[1];
					_this.addFlag = false;
					_this.isView = false;
					if (_this.$refs.refTable.selections.length !== 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
						return;
					}
					_this.dialogVisible = true;
					_this.viewTitle = _this.viewTitleArrry[1];
					_this.isReadOnly = false;
					var val = _this.$refs.refTable.selections[0];
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						_this.formdata.alarmClass = val.alarmClass;
						_this.formdata.ruleCode = val.ruleCode;
						_this.formdata.ruleName = val.ruleName;
						_this.formdata.resourceCode = val.resourceCode;
						_this.formdata.resourceAddr = val.resourceAddr;
						_this.formdata.waterLimit = val.waterLimit;
						_this.formdata.timeLimit = val.timeLimit;
						_this.formdata.validFlag = val.validFlag;
						_this.formdata.remark = val.remark;
						_this.queryTemplateDatas(_this.$refs.refTable.selections[0].alarmClass);
						_this.queryLoadDataObj(_this.$refs.refTable.selections[0]);
					});
				},
				deleteEvent: function () {

					var _this = this;
					if (_this.$refs.refTable.selections.length != 1) {
						_this.$message({ message: '请先至少选择一条记录', type: 'warning', duration: 6000 });
						return;
					}

					_this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning', duration: 6000,
						center: true,
						callback: function (action) {
							if (action === 'confirm') {
								yufp.service.request({
									method: 'POST',
									url: "/api/use/warning/resource/batchDelete",
									data: JSON.stringify(_this.$refs.refTable.selections),
									callback: function (code, message, response) {
										if (response.code === 0) {
											_this.reloadData();
											_this.$message('操作成功');
										} else {
											successFlag = false;
											_this.$message({ message: response.message, type: "warning" });
										}
									}
								});
							}
						}
					});
				},

				selectEvent: function (selection) {
					var _this = this;
					_this.selectCount = _this.$refs.refTable.selections.length;
				},
				stateFormatter: function (row, column, cellValue) {
					if (cellValue !== null) {
						return "已记录"
					} else {
						return "未记录"
					}
				},
				cancelFn: function () {
					var _this = this;
					this.dialogVisible = false;
					this.hintDialogVisible = false;
				},
				/**
				 * 点击树的结点，加载对应的数据
				 */
				nodeClickFn: function (nodeData, node, self) {

					var _this = this;
					_this.currentClickNode = nodeData;
					var alarmClass = "";
					if (nodeData.paramCode === '0000') {
						_this.addBtnShow = false;
						_this.isAppNode = false;
						_this.currentClickNode.alarmClass = "";

					} else {
						_this.addBtnShow = true;
						_this.isAppNode = true;
						_this.currAppNode = nodeData;
						_this.currentClickNode.alarmClass = nodeData.paramCode;
					}
					_this.reloadData();
				},
				filterMethodFn: function (row, column, val) {
					var text = '';
					this.templateDatas.forEach(function (item) {
						if (item.key === val) {
							text = item.value;
							return true;
						}
					});
					return text;
				},
				changeAlermMode: function (val) {

					if (val === 'S') {
						this.currTemplateDatas = this.templateDatasClass[0];
					} else if (val === 'M') {
						this.currTemplateDatas = this.templateDatasClass[1];
					} else if (val === 'N') {
						this.currTemplateDatas = this.templateDatasClass[2];
					} else if (val === '+') {
						this.currTemplateDatas = this.templateDatasClass[3];
					} else {
						this.currTemplateDatas.splice(0);
					}


				},
				/**
				 * 查询模版数据
				 */
				queryTemplateDatas: function (alarmClass) {
					var _this = this;
					var param = {};
					param.alarmClass = alarmClass;
					yufp.service.request({
						method: 'GET',
						url: "/api/use/warning/template/TemplateNoPage",
						data: { condition: JSON.stringify(param) },
						async: false,
						callback: function (code, message, response) {
							if (response.code !== 0) {
								_this.$message(response.message);
							} else {
								_this.templateDatas.splice(0);
								_this.templateDatasClass[0].splice(0);
								_this.templateDatasClass[1].splice(0);
								_this.templateDatasClass[2].splice(0);
								_this.templateDatasClass[3].splice(0);
								for (let i = 0; i < response.data.length; i++) {
									_this.templateDatas.push({ key: response.data[i].templateCode, value: response.data[i].templateName });
									if (response.data[i].alarmMode === 'S') {
										_this.templateDatasClass[0].push({ key: response.data[i].templateCode, value: response.data[i].templateName });
									} else if (response.data[i].alarmMode === 'M') {
										_this.templateDatasClass[1].push({ key: response.data[i].templateCode, value: response.data[i].templateName });
									} else {
										_this.templateDatasClass[2].push({ key: response.data[i].templateCode, value: response.data[i].templateName });
									}
									//对象组模版
									_this.templateDatasClass[3].push({ key: response.data[i].templateCode, value: response.data[i].templateName });
								}
							}

						}
					});
				},

				/**
				 * 查询模版数据
				 */
				queryLoadDataObj: function (obj) {
					var _this = this;
					yufp.service.request({
						method: 'GET',
						url: "/api/use/warning/resource/queryUseAlarmObject",
						data: obj,
						async: false,
						callback: function (code, message, response) {
							if (response.code !== 0) {
								_this.$message(response.message);
							} else {
								var result = response.data;
								_this.objDatas = result;
							}

						}
					});
				},
				nextStepFn: function () {
					var _this = this;

					var validate = false;
					_this.$refs.refForm.validate(function (valid) {
						validate = valid;
					});
					console.log(validate)
					if (!validate) {
						_this.$message({ message: "请填写必填项，请检查", type: "warning" });
						return;
					}
					_this.activeTabName = "configInfo";
				},
				lastStepFn: function () {
					this.activeTabName = "basicInfo";
				},
				saveFn: function () {
					var _this = this;

					var isvalid = true;

					if (_this.objDatas.length == 0) {
						_this.$message({ message: "对象配置不能为空", type: "warning" });
						return;
					}

					for (var i = 0; i < _this.objDatas.length; i++) {
						if (_this.objDatas[i].alarmMode === '+') {
							if (_this.objDatas[i].targetName === '' || _this.objDatas[i].templateCode === '' ||
								_this.objDatas[i].targetName === undefined || _this.objDatas[i].templateCode === undefined) {
								isvalid = false;
								break;
							}
						} else {
							if (_this.objDatas[i].targetName === '' || _this.objDatas[i].targetData === '' || _this.objDatas[i].templateCode === '' ||
								_this.objDatas[i].targetName === undefined || _this.objDatas[i].targetData === undefined || _this.objDatas[i].templateCode === undefined) {
								isvalid = false;
								break;
							}
						}
					}
					if (!isvalid) {
						_this.$message({ message: "对象名称、对象地址、警示模板不能为空，请检查", type: "warning" });
						return;
					}
					var paramObj = _this.$refs.refForm.formdata;
					console.log(paramObj)
					paramObj.useAlarmRuleList = _this.objDatas;
					let urlparam = "";
					if (_this.addFlag) {
						urlparam = 'api/use/warning/resource/';
					} else {
						urlparam = 'api/use/warning/resource/update';
					}
					yufp.service.request({
						method: 'POST',
						url: urlparam,
						data: paramObj,
						callback: function (code, message, response) {
							if (response.code === 0) {
								_this.reloadData();
								_this.dialogVisible = false;
								_this.$message("操作成功");
							} else {
								_this.$message(response.message);
							}
						}
					});
				},
				userChoosefn: function () {
					this.objectUserTabVisible = true;
				},
				addObjCfg: function () {
					var obj = { targetName: "", alarmMode: "", targetData: "", templateCode: "", remark: "" };
					obj.alarmMode = "S";
					this.objDatas.push(obj);
				},
				deleteObjCfg: function () {
					var _this = this;
					let selectDatas = _this.$refs.refObjTab.selections;
					for (let jdx = _this.objDatas.length - 1; jdx >= 0; jdx--) {
						for (let idx = 0; selectDatas.length > idx; idx++) {
							if (_this.objDatas[jdx] === selectDatas[idx]) {
								_this.objDatas.splice(jdx, 1);
								break;
							}
						}

					}
				},
				checkObjDataFn: function () {
					this.objectTabVisible = true;
				},
				/**
				 * 对象组配置
				 */
				checkGroupDataFn: function () {
					var _this = this;
					_this.groupTabVisible = true;
					var groupTargets = [];
					for (var i = 0; i < _this.objDatas.length; i++) {
						if (_this.objDatas[i].alarmMode === '+') {
							groupTargets.push(_this.objDatas[i].targetCode);
						}
					}
					_this.$nextTick(function () {
						_this.$refs.refGroupUsedTable.remoteData({ condition: { groupTargets: groupTargets } });
					})
				},
				setDisabled: function (row, column, cell, event) {
					if (row && row.alarmMode === "+" && column.property === "alarmMode") {
						var icon = cell.querySelector('.el-input__icon');
						cell.style.pointerEvents = 'none';
						if (icon) {
							icon.style.display = 'none';
						}
					}
				}
			}
		});
	};
});